# 作者：SP
# 开发时间：2025/3/14 17:38
# QDesktopWidget
import sys
from PyQt5.QtWidgets import QApplication,QDesktopWidget,QMainWindow
from PyQt5.QtGui import QIcon
class CenterFrom(QMainWindow):
    def __init__(self,Parent=None):
        super().__init__(Parent)

        #设置主窗口的标题
        self.setWindowTitle('居中显示')

        #设置窗口的尺寸
        self.resize(1200,800)
    def center(self):
        #获取屏幕坐标系
        screen = QDesktopWidget().screenGeometry()
        #获取窗口坐标系
        size = self.geometry()
        #计算窗口居中的左上角坐标
        newwidth = (screen.width() - size.width())/2
        newheight = (screen.height() - size.height())/2
        self.move(newwidth,newheight)                      #实例方法，让窗口居中

if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setWindowIcon(QIcon('123.jpg'))
    centerform = CenterFrom()
    centerform.show()

    sys.exit(app.exec_())